﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;
using System.Net.Mail;

namespace HCFramework.News
{
    public partial class CategoryManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            HCDataContext dc = new HCDataContext();
            RadGridMain.DataSource = dc.NewsCategories.ToList();
            RadGridMain.DataBind();
        }

        protected void RadGridMain_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Footer)
            {
                
            }
        }

        protected void RadGridMain_ItemCommand(object source, GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Them":
                    NewsCategory nc = new NewsCategory();
                    nc.Code = (e.Item.FindControl("txtCode") as TextBox).Text;
                    nc.Name = (e.Item.FindControl("txtName") as TextBox).Text;
                    C.NewsCategoryController.Insert(nc);
                    break;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int id = int.Parse(item["ID"].Text);
                TextBox txtCode = item.FindControl("txtCode") as TextBox;
                TextBox txtName = item.FindControl("txtName") as TextBox;

                HCDataContext db = new HCDataContext();
                NewsCategory nc = C.NewsCategoryController.GetByID(id, db);
                nc.Code = txtCode.Text;
                nc.Name = txtName.Text;
                C.NewsCategoryController.Update(nc, db);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem item in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)item;
                int id = int.Parse(dataItem["ID"].Text);

                C.NewsCategoryController.Delete(id);
            }
        }
    }
}